package org.edu.modules.classChoise.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.classChoise.entity.VeJwXksj;
import org.edu.modules.classChoise.service.IVeJwXksjService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: 选课时间
 * @Author:
 * @Date:   2021-05-14
 * @Version: V1.0
 */
@Api(tags="选课时间")
@RestController
@RequestMapping("/classChoise/veJwXksj")
@Slf4j
public class VeJwXksjController extends BaseController<VeJwXksj, IVeJwXksjService> {
	@Autowired
	private IVeJwXksjService veJwXksjService;
	
	/**
	 * 分页查询
	 * @param veJwXksj
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "选课时间-分页查询")
	@ApiOperation(value="分页查询", notes="选课时间-分页查询")
	@PostMapping(value = "/list")
	public Result<?> queryPageList(VeJwXksj veJwXksj,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        if(veJwXksj.getIsdelete()==null){
            veJwXksj.setIsdelete(0);
        }
		QueryWrapper<VeJwXksj> queryWrapper = QueryGenerator.initQueryWrapper(veJwXksj, req.getParameterMap());
		Page<VeJwXksj> page = new Page<VeJwXksj>(pageNo, pageSize);
		IPage<VeJwXksj> pageList = veJwXksjService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	* 通过id查询
	* @param id
	* @return
	*/
	@AutoLog(value = "选课时间-通过id查询")
	@ApiOperation(value="通过id查询", notes="选课时间-通过id查询")
	@PostMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwXksj veJwXksj = veJwXksjService.getById(id);
		if(veJwXksj==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(veJwXksj);
	}
	
	/**
	 * 新增信息
	 * @param veJwXksj
	 * @return
	 */
	@AutoLog(value = "选课时间-新增信息")
	@ApiOperation(value="新增信息", notes="选课时间-新增信息")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwXksj veJwXksj) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(sysUser!=null){
            veJwXksj.setCreateuserid(sysUser.getId());
            veJwXksj.setCreateusername(sysUser.getRealname());
        }
        veJwXksj.setCreatetime(new Date());
		if(veJwXksjService.save(veJwXksj)){
		   return Result.OK("添加成功！",veJwXksj.getId());
        }
        return Result.error("添加失败!");
	}
	
	/**
	 * 修改信息
	 * @param veJwXksj
	 * @return
	 */
	@AutoLog(value = "选课时间-修改信息")
	@ApiOperation(value="修改信息", notes="选课时间-修改信息")
	@PostMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwXksj veJwXksj) {
        veJwXksj.setUpdatetime(new Date());
        if(veJwXksjService.updateById(veJwXksj)){
		    return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败!");
	}
	
//	/**
//	 * 通过id删除
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "选课时间-通过id删除")
//	@ApiOperation(value="通过id删除", notes="选课时间-通过id删除")
//	@PostMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//        if(veJwXksjService.removeById(id)){
//		    return Result.OK("删除成功!");
//        }
//        return Result.error("删除失败!");
//	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "选课时间-批量销毁")
	@ApiOperation(value="批量销毁", notes="选课时间-批量销毁")
	@PostMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		if(this.veJwXksjService.removeByIds(Arrays.asList(ids.split(",")))){
		         return Result.OK("批量销毁成功!");
        }
        return Result.error("批量销毁失败!");
	}
     /**
      *
      * @param ids
      * @return
      */
     @AutoLog(value = "选课时间-批量删除")
     @PostMapping("deleteByStatus")
     @ApiOperation(value = "批量删除", notes = "调课信息-批量删除")
     public Result<?> deleteByStatus(@RequestParam(name="ids",required=true) String ids)
     {
         if(this.veJwXksjService.changeDelStatusByIds(ids.split(","),1)>0){
             return Result.OK("操作成功!");
         }
         return Result.error("操作失败");
     }

     /**
      *
      * @param ids
      * @return
      */
     @AutoLog(value = "选课时间-批量还原")
     @PostMapping("reverseByStatus")
     @ApiOperation(value = "批量还原", notes = "调课信息-批量还原")
     public Result<?> reverseByStatus(@RequestParam(name="ids",required=true) String ids)
     {
         if(this.veJwXksjService.changeDelStatusByIds(ids.split(","),0)>0){
             return Result.OK("操作成功!");
         }
         return Result.error("操作失败");
     }
	 /**
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "选课时间-设置有效")
	 @PostMapping("validStatus")
	 @ApiOperation(value = "设置有效", notes = "调课信息-设置有效")
	 public Result<?> validStatus(@RequestParam(name="ids",required=true) String ids)
	 {
		 if(this.veJwXksjService.changeStatusByIds(ids.split(","),1)>0){
			 return Result.OK("操作成功!");
		 }
		 return Result.error("操作失败");
	 }

	 /**
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "选课时间-批量还原")
	 @PostMapping("invalidStatus")
	 @ApiOperation(value = "设置失效", notes = "调课信息-设置失效")
	 public Result<?> invalidStatus(@RequestParam(name="ids",required=true) String ids)
	 {
		 if(this.veJwXksjService.changeStatusByIds(ids.split(","),0)>0){
			 return Result.OK("操作成功!");
		 }
		 return Result.error("操作失败");
	 }

     /**
      * 导出excel
      *
      * @param request
      * @param veJwXksj
      */
     @RequestMapping(value = "/exportXls")
     public ModelAndView exportXls(HttpServletRequest request,   VeJwXksj veJwXksj) {
         return super.exportXls(request, veJwXksj, VeJwXksj.class, "选课时间信息");
     }

//     /**
//      * 导出excel
//      *
//      * @param request
//      * @param veJwXksj
//      */
//     @AutoLog(value = "选课时间-导出Excel")
//     @ApiOperation(value = "导出Excel", notes = "选课时间-导出Excel")
//     @GetMapping(value = "/exportXls")
//     //@RequestMapping(value = "/exportXls")
//     public ModelAndView exportXls(HttpServletRequest request, VeJwXksj veJwXksj) {
//         // Step.1 组装查询条件
//         QueryWrapper<VeJwXksj> queryWrapper = QueryGenerator.initQueryWrapper(veJwXksj, request.getParameterMap());
//         //Step.2 AutoPoi 导出Excel
//         ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//         List<VeJwXksj> pageList = veJwXksjService.list(queryWrapper);
//         //导出文件名称
//         mv.addObject(NormalExcelConstants.FILE_NAME,"选课时间");
//         mv.addObject(NormalExcelConstants.CLASS,VeJwXksj.class);
//         //LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//         //mv.addObject(NormalExcelConstants.PARAMS,new ExportParams("角色列表数据","导出人:"+user.getRealname(),"导出信息"));
//         mv.addObject(NormalExcelConstants.DATA_LIST,pageList);
//         return mv;
//         //return super.exportXls(request, veJwXkxz, VeJwXkxz.class, "教务课程信息");
//     }

 }
